有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何检查给定的时间是否在时间限制之间

下面是交易清单,我需要每15分钟进行一次交易计数,00:01到00:15,00:16到00:30,00:31到00:45。。。23:45(直到一天结束),如果在该特定时间没有交易,则需要填充0.00

样本数据:

124385 20191029001650
124385 20191029002050
124385 20191029102050
124391 20191029135007
124391 20191029135507 
124392 20191029144229

预期输出应为0.00,2,0.00,0.00,0.00 ....

我们需要发送数据,如00:15至00:30“2”交易已完成,以及10:16至10:30“1”交易


共 (1) 个答案

  1. # 1 楼答案

    定义一个带有两个成员字段的event类:一个Long整数对象用于事务编号,另一个LocalDateTime for the date-time value (or即时`如果您知道预期的时区或UTC偏移量)

    package work.basil.example;
    
    import java.time.LocalDateTime;
    import java.util.Objects;
    
    final public class Event implements Comparable < Event >
    {
        //    -|  Members  |                 -
        final private Integer transactionId;
        final private LocalDateTime when;
    
        //    -|  Constructors  |                 -
        public Event ( Integer transactionId , LocalDateTime when )
        {
            this.transactionId = Objects.requireNonNull( transactionId );
            this.when = Objects.requireNonNull( when );
        }
    
        //    -|  Accessors  |                 -
        // Immutable, read-only. So getters only, no setters.
        public Integer getTransactionId ( )
        {
            return transactionId;
        }
    
        public LocalDateTime getWhen ( )
        {
            return when;
        }
    
        //    -|  Object  |                 -
    
        @Override
        public boolean equals ( Object o )
        {
            if ( this == o ) return true;
            if ( o == null || getClass() != o.getClass() ) return false;
            Event event = ( Event ) o;
            return getTransactionId().equals( event.getTransactionId() ) &&
                    getWhen().equals( event.getWhen() );
        }
    
        @Override
        public int hashCode ( )
        {
            return Objects.hash( getTransactionId() , getWhen() );
        }
    
        @Override
        public String toString ( )
        {
            return "Event{ " +
                    "transactionId=" + transactionId +
                    " | when=" + when +
                    " }";
        }
    }
    

    解析传入的数据

    对于ID号,使用Integer类进行解析

    Integer transactionId = Integer.valueOf( input ) ;
    

    对于日期时间,定义格式模式

    DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHmmss" ) ;
    

    解析每个字符串

    LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
    

    按每个LocalDateTime对对象进行排序。实现Comparable或生成Comparator对象。这两个问题在堆栈溢出中都被多次提及,所以请搜索以了解更多信息

    //     |  Comparator  |           -
    /**
     * Comparator by `when`
     */
    public static Comparator <Event> WhenComparator = new Comparator<Event>() {
    
        @Override
        public int compare(Event e1, Event e2) {
            return e1.getWhen().compareTo( e2.getWhen() );
        }
    };
    

    从第一个对象开始。以它的LocalDateTime对象为例。通过调用with并传递LocalTime.MIN来创建一个新的。没有区域或偏移,我们知道每天和每小时都是一个通用长度,60分钟,24小时

    所以循环,每次增加15分钟。使用^{}以小时-分钟-秒为单位定义未附加到时间线的时间跨度

    Duration d = Duration.ofMinutes( 15 ) ;
    

    这会给你一个开始和停止的时间

    • 如果要将这些LocalDateTime对象调整为偏移量或时区,请为开始和停止提取Instant。然后在将ThreeTen-Extra库添加到项目中之后,将它们作为^{}对象包含。Interval类提供了方便的比较方法,例如contains
    • 如果使用^ {CD5> },请考虑自己的^ {< CD18> }类来保存一对^ {< CD5> }对象,并实现^ {

    您自己的LocalDateTimeRange可能看起来像这个完全未经测试的源代码草稿:

    package work.basil.example;
    
    import java.time.LocalDateTime;
    import java.util.Objects;
    
    public class LocalDateTimeRange
    {
        final private LocalDateTime start, end;
    
        public LocalDateTimeRange ( LocalDateTime start , LocalDateTime end )
        {
            this.start = Objects.requireNonNull( start );
            this.end = Objects.requireNonNull( end );
        }
    
        public LocalDateTime getStart ( )
        {
            return this.start;
        }
    
        public LocalDateTime getEnd ( )
        {
            return this.end;
        }
    
        public boolean contains ( LocalDateTime ldt )
        {
            // Contains is true if the target is greater-than-or-equal-to (not before) the start, and is less-than (before) the ending. 
            // This approach is known as Half-Open, where the beginning is inclusive while the ending is exclusive. 
            return ( ! ldt.isBefore( this.getStart() ) ) && ldt.isBefore( this.getEnd() );
        }
    
        @Override
        public boolean equals ( Object o )
        {
            if ( this == o ) return true;
            if ( o == null || getClass() != o.getClass() ) return false;
            LocalDateTimeRange that = ( LocalDateTimeRange ) o;
            return getStart().equals( that.getStart() ) &&
                    getEnd().equals( that.getEnd() );
        }
    
        @Override
        public int hashCode ( )
        {
            return Objects.hash( getStart() , getEnd() );
        }
    
        @Override
        public String toString ( )
        {
            return start + "/" + end; // Return string in standard ISO 8601 format. 
        }
    
    
        static public LocalDateTimeRange parse ( )
        {
            // Parse a string in standard ISO 8601 format. 
            …
        }
    }
    

    比较手上的物体,看它是否不小于起点和终点。这种半开放式的定义时间跨度的方法通常是最好的,即开始是包容性的,而结束是排他性的

    继续循环,直到找到包含手头对象的日期时间的日期时间范围。找到后,将以下内容添加到Map

    • 增加计数的Map < LocalDateTimeRange , < Integer > >
    • 一个Map < LocalDateTimeRange , < List< Event > > >,您可以将Event对象添加到ListSet

    if there is no transaction done at that particular time need to populate 0.0

    要么将这样一个值添加到映射中,要么将映射的保留为空,用于该LocalDateRange

    循环到下一个对象

    完成后,分析地图

    提示:教育发布该数据的人员:

    • ISO 8601序列化为文本的日期时间值的标准格式
    • 始终指示时区或UTC偏移的重要性

    上面讨论的所有这些主题都已在堆栈溢出上多次讨论过。因此,搜索以了解更多信息。并在发布前彻底搜索堆栈溢出